package main.com.oaer.util;

import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import main.com.oaer.model.User;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


public class CommonUtil {
	
	private static final JsonConfig jsonConfig = new JsonConfig();
	private static final JsonValueProcessor jsonValueProcessor= new JsonValueProcessorImpl();
	private static final JsonValueProcessor jsonValueProcessor2= new JsonValueProcessorImpl("yyyy-MM-dd");
	
	
	/**
	 * 通过HttpServletResponse发出一个对象，并转换成json 形式
	 * @param response
	 * @param obj
	 * @param content
	 */
    public static void printToHTML(HttpServletResponse response, Object obj, String content){
	   PrintWriter pw = null;
	   response.setContentType(content);
	   try{
		   pw = response.getWriter();
		   pw.print(toJsonByObject(obj,false));
	   }catch(Exception e){
		   throw new RuntimeException(e);
	   }finally{
		  pw.close(); 
	   }
   }
    /**
     * 将对象转换成json对象
     * @param obj
     * @param flag
     * @return
     */
    private static JSONObject toJsonByObject(Object obj, boolean flag) {
	if(flag){
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, jsonValueProcessor2);
		JSONObject.fromObject(obj, jsonConfig);
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, jsonValueProcessor2);
		JSONObject.fromObject(obj, jsonConfig);
		jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class, jsonValueProcessor2);
		return JSONObject.fromObject(obj, jsonConfig);
	}else{
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, jsonValueProcessor);
		JSONObject.fromObject(obj, jsonConfig);
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, jsonValueProcessor);
		JSONObject.fromObject(obj, jsonConfig);
		jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class, jsonValueProcessor);
		return JSONObject.fromObject(obj, jsonConfig);
	}
   }
    
    /**
     * 通过HttpServletResponse 发送一个收集，并且转换成jsonarray形式
     * @param response
     * @param collection
     */
    public static void printToHTML(HttpServletResponse response, Collection<Object> collection){
    	printToHTML(response, collection, "text/html;charset=UTF-8");
    }
    
    /**
     * 通过HttpServletResponse 发送一个对象，并且转换成json形式
     * @param response
     * @param object
     */
    public static void printToHTML(HttpServletResponse response, Object object){
    	printToHTML(response, object, "text/html;charset=UTF-8");
    }
    
    /**
     * 字符串数组转换成长整型数组
     * @param sid
     * @return
     */
    public static Long[] stringToLong(String[] sid){
    	  Long[] lid = new Long[sid.length];
          for (int i = 0; i < sid.length; i++) { 
        	  lid[i] = Long.parseLong(sid[i]);      
          }
          return lid;
    } 
    
    /**
     * 判断字符串是否为空或者为空字符串
     * @param string
     * @return
     */
    public static boolean stringNotEmpty(String string){
         if( string!=null && !("").equals(string)){
			return true;
		}else{
			return false;
		}
    }
   
    public static User getUserFromSession(HttpSession session){
    	return (User)session.getAttribute("userMap");
    }
}
